package com.atguigu.controller;


import com.alibaba.fastjson.JSONObject;
import com.atguigu.entity.BaseCategory1;
import com.atguigu.entity.BaseCategory2;
import com.atguigu.entity.BaseCategory3;
import com.atguigu.result.RetVal;

import com.atguigu.service.BaseCategory1Service;
import com.atguigu.service.BaseCategory2Service;
import com.atguigu.service.BaseCategory3Service;
import com.atguigu.service.BaseCategoryViewService;
import com.atguigu.service.impl.BaseCategory1ServiceImpl;
import com.atguigu.service.impl.BaseCategory2ServiceImpl;
import com.atguigu.service.impl.BaseCategory3ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 一级分类表 前端控制器
 * </p>
 *
 * @author fumeng
 * @since 2021-10-28
 */
//@CrossOrigin
@RestController
@RequestMapping("/product")
public class CategoryController {

    @Autowired
    BaseCategory1Service category1Service;
    @Autowired
    BaseCategory2Service category2Service;
    @Autowired
    BaseCategory3Service category3Service;
    @Autowired
    BaseCategoryViewService baseCategoryViewService;
    //查询一级分类
    @GetMapping("getCategory1")
    public RetVal getCategory1(){
        List<BaseCategory1> category1List = category1Service.list(null);
        return RetVal.ok(category1List);
    }
    //查询二级分类
    @GetMapping("getCategory2/{category1Id}")
    public RetVal getCategory2(@PathVariable Long category1Id){
        QueryWrapper<BaseCategory2> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category1_id",category1Id);
        List<BaseCategory2> list = category2Service.list(queryWrapper);
        return RetVal.ok(list);
    }
    //查询三级分类
    @GetMapping("getCategory3/{category2Id}")
    public RetVal getCategory3(@PathVariable Long category2Id){

        QueryWrapper<BaseCategory3> wrapper = new QueryWrapper<>();
        wrapper.eq("category2_id",category2Id);
        List<BaseCategory3> list1 = category3Service.list(wrapper);
        return RetVal.ok(list1);
    }


    //查询首页分类信息
    @GetMapping("getIndexCategoryInfo")
    public RetVal getIndexCategoryInfo(){

       List<JSONObject> categoryInfo=baseCategoryViewService.getIndexCategoryInfo();
       return RetVal.ok(categoryInfo);
    }


}

